const http = require('http')
const app = http.createServer(handler)
const io = require('socket.io')(app)
const fs = require('fs')


function handler(req, res) {
  fs.readFile('./index.html', (err, data) => {
    if (err) {
      console.log(err);
      return res.end('error loading index.html')
    }

    res.end(data)
  })
}

io.on('connection', function (socket) {
  console.log('一个客户端连接了');
  io.emit('news', 'hello world')
  socket.on('event', function (data) {
    console.log(data);
  })
})

app.listen(3000,'127.0.0.1')